class Idea < ActiveRecord::Base
  attr_accessor :requirments
  belongs_to :creator, :class_name => "User"
  has_many :idea_invitations
  has_many :invited_users, :through => :idea_invitations, :source => :user
  has_many :participations
  has_many :participants, :through => :participations, :source => :user
  has_many :needefiers
  has_many :needs, :through => :needefiers, :source => :speciality
  has_many :comments
  
  
  validates :name,:abstract, :presence =>true
  

  
  scope :featured, where(:featured => true)
  scope :recent, :order => "created_at desc"
  
  def requirments
    if needs.present?
      needs.map(&:name).to_json
    else
      ""
    end
  end
  
  def requirments=(value)
    needs_names = value.split(',')
    self.needs = needs_names.collect { |n| Speciality.find_or_create_by_name n }
  end
  
end
